using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    [ToolboxData("<{0}:DateTimeField runat=server></{0}:DateTimeField>")]
    public class DateField : BaseDataField
    {
        public DateField() : base()
        { 
            base.DataFormatString = string.Concat("{0:", System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern, "}");
        }

        protected override DataControlField CreateField()
        {
            return new DateField();
        }

        private DateTime minValue;
        public DateTime MinValue
        {
            get { return minValue; }
            set { minValue = value; }
        }

        private DateTime maxValue;
        public DateTime MaxValue
        {
            get { return maxValue; }
            set { maxValue = value; }
        }

        protected override object SaveViewState()
        {
            ViewState["MinValue"] = MinValue;
            ViewState["MaxValue"] = MaxValue;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            MinValue= (DateTime)ViewState["MinValue"];
            MaxValue = (DateTime)ViewState["MaxValue"];
        }

        protected override void InitEditControls(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            Telerik.WebControls.RadDatePicker ctrl = new Telerik.WebControls.RadDatePicker();
            ctrl.ID = base.GetUniqueId(rowIndex, "date");
            cell.Controls.Add(ctrl);
            base.InitEditControls(cell, cellType, rowState, rowIndex);
        }

        protected override void DataBindEditControls(DataControlFieldCell cell, object dataItem, int dataItemIndex, int displayIndex)
        {
            Telerik.WebControls.RadDatePicker ctrl = cell.FindControl(GetUniqueId(displayIndex, "date")) as Telerik.WebControls.RadDatePicker;
            if (ctrl != null)
            {
                if (MinValue != DateTime.MinValue)
                    ctrl.MinDate = MinValue;
                if (MaxValue != DateTime.MinValue)
                    ctrl.MaxDate = MaxValue;
                ctrl.SelectedDate = DataBinder.Eval(dataItem, DataField) as DateTime?;
            }
            base.DataBindEditControls(cell, dataItem, dataItemIndex, displayIndex);
        }

        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            Telerik.WebControls.RadDatePicker ctrl =  cell.Controls[0] as Telerik.WebControls.RadDatePicker;
            if (ctrl != null)
            {
                dictionary.Add(DataField, ctrl.SelectedDate); 
            }
        }
    }
}
